package com.ruoyi.report_ys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_tg.domain.TdjsjlbBaDetail;
import com.ruoyi.report_ys.domain.ReportYsDzkyqdsyBaDetail;
import com.ruoyi.report_ys.domain.ReportYsDzkyqdsybgBaDetail;
import com.ruoyi.report_ys.domain.bo.ReportYsDzkyqdsyBaDetailBo;
import com.ruoyi.report_ys.domain.vo.ReportYsDzkyqdsyBaDetailVo;
import com.ruoyi.report_ys.domain.vo.ReportYsDzkyqdsybgBaDetailVo;
import com.ruoyi.report_ys.mapper.ReportYsDzkyqdsyBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ys.domain.bo.ReportYsDzkyqdsyBsInfoBo;
import com.ruoyi.report_ys.domain.vo.ReportYsDzkyqdsyBsInfoVo;
import com.ruoyi.report_ys.domain.ReportYsDzkyqdsyBsInfo;
import com.ruoyi.report_ys.mapper.ReportYsDzkyqdsyBsInfoMapper;
import com.ruoyi.report_ys.service.IReportYsDzkyqdsyBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 岩石单轴抗压强度试验检测记录主Service业务层处理
 *
 * @author zyb
 * @date 2024-10-28
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportYsDzkyqdsyBsInfoServiceImpl implements IReportYsDzkyqdsyBsInfoService {

    private final ReportYsDzkyqdsyBsInfoMapper baseMapper;
    private final ReportYsDzkyqdsyBaDetailMapper detailMapper;

    /**
     * 查询岩石单轴抗压强度试验检测记录主
     */
    @Override
    @FIllBasicInfo
    public ReportYsDzkyqdsyBsInfoVo queryById(Long excelId) {
        ReportYsDzkyqdsyBsInfoVo reportYsDzkyqdsyBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportYsDzkyqdsyBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportYsDzkyqdsyBaDetail>()
            .eq(ReportYsDzkyqdsyBaDetail::getExcelId, excelId));
        if (CollectionUtils.isNotEmpty(baDetailVos)){
            reportYsDzkyqdsyBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return reportYsDzkyqdsyBsInfoVo;
    }

    /**
     * 查询岩石单轴抗压强度试验检测记录主列表
     */
    @Override
    public TableDataInfo<ReportYsDzkyqdsyBsInfoVo> queryPageList(ReportYsDzkyqdsyBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportYsDzkyqdsyBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportYsDzkyqdsyBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询岩石单轴抗压强度试验检测记录主列表
     */
    @Override
    public List<ReportYsDzkyqdsyBsInfoVo> queryList(ReportYsDzkyqdsyBsInfoBo bo) {
        LambdaQueryWrapper<ReportYsDzkyqdsyBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportYsDzkyqdsyBsInfo> buildQueryWrapper(ReportYsDzkyqdsyBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportYsDzkyqdsyBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportYsDzkyqdsyBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportYsDzkyqdsyBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportYsDzkyqdsyBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportYsDzkyqdsyBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportYsDzkyqdsyBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportYsDzkyqdsyBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportYsDzkyqdsyBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportYsDzkyqdsyBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportYsDzkyqdsyBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportYsDzkyqdsyBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportYsDzkyqdsyBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportYsDzkyqdsyBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportYsDzkyqdsyBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportYsDzkyqdsyBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportYsDzkyqdsyBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportYsDzkyqdsyBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportYsDzkyqdsyBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportYsDzkyqdsyBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportYsDzkyqdsyBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportYsDzkyqdsyBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportYsDzkyqdsyBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyyt()), ReportYsDzkyqdsyBsInfo::getShiyyt, bo.getShiyyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYansclms()), ReportYsDzkyqdsyBsInfo::getYansclms, bo.getYansclms());
        lqw.eq(StringUtils.isNotBlank(bo.getShijxz()), ReportYsDzkyqdsyBsInfo::getShijxz, bo.getShijxz());
        lqw.eq(bo.getPingjkyqd1() != null, ReportYsDzkyqdsyBsInfo::getPingjkyqd1, bo.getPingjkyqd1());
        lqw.eq(bo.getPingjkyqd2() != null, ReportYsDzkyqdsyBsInfo::getPingjkyqd2, bo.getPingjkyqd2());
        return lqw;
    }

    /**
     * 新增岩石单轴抗压强度试验检测记录主
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportYsDzkyqdsyBsInfoBo bo) {
        ReportYsDzkyqdsyBsInfo add = BeanUtil.toBean(bo, ReportYsDzkyqdsyBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详情 12 次
            ArrayList<ReportYsDzkyqdsyBaDetail> details = new ArrayList<>(12);
            for (long i = 1; i <= 12L; i++) {
                details.add(new ReportYsDzkyqdsyBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改岩石单轴抗压强度试验检测记录主
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportYsDzkyqdsyBsInfoBo bo) {
        ReportYsDzkyqdsyBsInfo update = BeanUtil.toBean(bo, ReportYsDzkyqdsyBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), ReportYsDzkyqdsyBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportYsDzkyqdsyBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除岩石单轴抗压强度试验检测记录主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
